﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Sunny.Controls
{
	public class TextSearchItem : SearchItem
	{
		private System.Windows.Controls.TextBox _SimpleSearcherInput;
		public override System.Windows.FrameworkElement SimpleSearcherInput
		{
			get
			{
				if (this._SimpleSearcherInput == null)
					this._SimpleSearcherInput = new System.Windows.Controls.TextBox { Width = 100 };
				return this._SimpleSearcherInput;
			}
		}

		public string Text
		{
			get
			{
				return ((System.Windows.Controls.TextBox)this.SimpleSearcherInput).Text;
			}
			set
			{
				((System.Windows.Controls.TextBox)this.SimpleSearcherInput).Text = value;
			}
		}

		/*
	{ Text: "包含", Value: "{0}.Contains({1})" },
	{ Text: "不包含", Value: "!{0}.Contains({1})" },
	{ Text: "等于", Value: "{0}=={1}" },
	{ Text: "始于", Value: "{0}.StartsWith({1})" },
	{ Text: "非始于", Value: "!{0}.StartsWith({1})" },
	{ Text: "终于", Value: "{0}.EndsWith({1})" },
	{ Text: "非终于", Value: "!{0}.EndsWith({1})" }
		 */
		private Operator[] _OperatorOptions;
		public override Operator[] Operators
		{
			get
			{
				if (this._OperatorOptions == null)
					this._OperatorOptions = new Operator[]
					{
						new Operator("包含","{0}.Contains({1})"),
						new Operator("不包含","!{0}.Contains({1})"),
						new Operator("等于","{0}=={1}"),
						new Operator("始于","{0}.StartsWith({1})"),
						new Operator("非始于","!{0}.StartsWith({1})"),
						new Operator("终于","{0}.EndsWith({1})"),
						new Operator("非终于","!{0}.EndsWith({1})")
					};
				return this._OperatorOptions;
			}
		}

		public override System.Windows.FrameworkElement AdvanceSearcherInput
		{
			get
			{
				return new System.Windows.Controls.TextBox { Width = 100 };
			}
		}

		public override Searcher.WhereCommand GetCommand(string field, int index)
		{
			if (string.IsNullOrEmpty(this.Text))
				return null;
			return new Searcher.WhereCommand { Text = string.Format("{0}.Contains(@{1})", field, index), Parameters = new object[] { this.Text } };
		}

		public override void Empty()
		{
			if (this._SimpleSearcherInput != null)
				this._SimpleSearcherInput.Text = "";
		}
	}
}